package com.wt.struggle.goodbye.message

import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import android.view.ViewGroup
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProBaseAdapter
import com.wt.struggle.goodbye.info.MessageInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.main.activity.AddCommentActivity
import com.xin.lv.yang.utils.utils.DataUtil
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.message_item.view.*

class MessageAdapter(context: Context, list: ArrayList<MessageInfo>) : ProBaseAdapter<MessageInfo>(context, list) {

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        val vh = holder as VH
        val info = list[position]

        if (position == 0) {
            vh.tiLinear.visibility = View.VISIBLE
        } else {
            vh.tiLinear.visibility = View.GONE
        }


        Log.i("result", "---type----" + info.type)

        if (info.type == 2) {

            vh.tvTextHuiFu.visibility = View.VISIBLE

            vh.tvMessageStyle.text = "私信"

            if (info.fromUserInfo != null) {

                vh.msgName.text = info.fromUserInfo!!.nickName

                ImageUtil.getInstance()
                    .loadCircleImage(
                        context,
                        vh.msgHead,
                        Config.GET_IMAGE_URL + info.fromUserInfo!!.avatar,
                        R.mipmap.logo_small
                    )
            }


        } else if (info.type == 1) {

            vh.tvTextHuiFu.visibility = View.GONE
            vh.tvMessageStyle.text = "消息"
            vh.msgHead.setImageBitmap(BitmapFactory.decodeResource(context.resources, R.mipmap.logo_small))
        }

        vh.tvMessageMs.text = DataUtil.getEmoji(context, info.content)

        val time = info.createTime

        vh.tvMessageSendTime.text = DataUtil.longToTime(time!!.toLong() / 1000, "yyyy-MM-dd")

        val state = info.status

        Log.i("result", "---state----$state")

        //  1 为未读   2 为已读
        if (state == 1) {
            vh.imagePoint.visibility = View.VISIBLE
        } else {
            vh.imagePoint.visibility = View.GONE
        }

        vh.tvTextHuiFu.setOnClickListener {
            val intent = Intent(context, AddCommentActivity::class.java)
            intent.putExtra("code", 3)
            intent.putExtra("id", info.id)
            intent.putExtra("toId", info.fromId)
            context.startActivity(intent)
        }
    }

    override fun onCreateView(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return VH(inflater.inflate(R.layout.message_item, parent, false))
    }

    override fun onUpdateHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any>) {

    }

    class VH(view: View) : RecyclerView.ViewHolder(view) {
        val tiLinear = view.tiLinear
        val tvMessageStyle = view.tvMessageStyle
        val msgHead = view.msgHead
        val msgName = view.msgName
        val tvMessageMs = view.tvMessageMs
        val tvMessageSendTime = view.tvMessageSendTime
        val imagePoint = view.imagePoint
        val tvTextHuiFu = view.tvTextHuiFu

    }
}